package com.rabbitmq.demo.ttl;

import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageBuilder;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;

public class Sender {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private DirectExchange directExchange;

    @Scheduled(fixedDelay = 2000, initialDelay = 500)
    public void send(){
        Message message = MessageBuilder.withBody("Hello,RabbitMQ!".getBytes()).setExpiration("1000").build();
        System.out.println("send message: " + new String(message.getBody()));
        rabbitTemplate.send(directExchange.getName(),"ttl.queue",message);
    }

    @Scheduled(fixedDelay = 4000, initialDelay = 500)
    public void send1(){
        rabbitTemplate.convertAndSend(directExchange.getName(),"ttlQueue","Hello,World!");
    }
}
